package net.chasing.androidbaseconfig.util;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * 软键盘显示隐藏的监听
 */
public class SoftKeyBoardListener {
    private View rootView;//activity的根视图
    private int rootViewVisibleHeight;//纪录根视图的显示高度
    private Rect rect;
    private ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener;
    private int screenHeight;
    // 当前软键盘是否展示，判断是否切换了状态，避免频繁回调listener
    private boolean isShow = false;

    public SoftKeyBoardListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();
        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        globalLayoutListener = () -> {
            //获取当前根视图在屏幕上显示的大小
            rect = new Rect();
            rootView.getWindowVisibleDisplayFrame(rect);
            int visibleHeight = rect.height();
            L.i("visibleHeight: " + visibleHeight);

            boolean inMultiWindowMode = ActivityUtils.isInMultiWindowMode(activity);
            if (inMultiWindowMode) {
                if (rootViewVisibleHeight == 0)
                    rootViewVisibleHeight = visibleHeight;

                // 几个像素内的变化忽视，可能是抖动产生
                if (Math.abs(rootViewVisibleHeight - visibleHeight) > 6) {
                    // 小窗口/分屏模式下，软键盘不占用Activity高度
                    if (onSoftKeyBoardChangeListener != null) {
                        if (rootViewVisibleHeight > visibleHeight)
                            onSoftKeyBoardChangeListener.keyBoardShow(0);
                        else
                            onSoftKeyBoardChangeListener.keyBoardHide(0);
                    }
                    rootViewVisibleHeight = visibleHeight;
                }
                return;
            }

            screenHeight = GenericTools.getScreenHeight(activity);
            if (visibleHeight > screenHeight)
                visibleHeight = screenHeight;
            if (rootViewVisibleHeight == 0) {
                rootViewVisibleHeight = visibleHeight;
                if (!isShow && rootViewVisibleHeight < screenHeight - 400) {
                    isShow = true;
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(screenHeight - rootViewVisibleHeight);
                    }
                }
                return;
            }

            //根视图显示高度没有变化，可以看作软键盘显示／隐藏状态没有改变
            if (rootViewVisibleHeight == visibleHeight) {
                return;
            }

            //根视图显示高度变小超过200，可以看作软键盘显示了
            if (rootViewVisibleHeight - visibleHeight > 100) {
                if (!isShow && onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                }
                isShow = true;
                rootViewVisibleHeight = visibleHeight;
            } else if (visibleHeight - rootViewVisibleHeight > 100) {
                //根视图显示高度变大超过200，可以看作软键盘隐藏了
                if (isShow && onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener.keyBoardHide(0);
                }
                isShow = false;
                rootViewVisibleHeight = visibleHeight;
            }
        };
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public void unRegister() {
        if (globalLayoutListener != null) {
            rootView.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
            rootView = null;
            globalLayoutListener = null;
        }
    }
}
